<template>
  <div class="pages bjmember">
    <div class="from_main">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="等级名称" prop="name">
          <el-input v-model="ruleForm.name" :disabled="editdisabled" size="small" placeholder="最多15个字符" clearable maxlength="15"></el-input>
        </el-form-item>
        <el-form-item label="等级说明：" prop="levelDescribe">
          <textarea maxlength="20" v-model="ruleForm.levelDescribe" name id cols="10" rows="5" :disabled="editdisabled1" placeholder="输入等级说明，最多20字" style="width:220px;resize:none;border-radius:5px;"></textarea>
        </el-form-item>
        <el-form-item label="会员优惠：" prop="memberDiscount" class="memberStyle">
          <el-radio-group v-model="ruleForm.discountType" @change="changePrice" :disabled="editdisabled">
            <div style="margin-top:10px;">
              <el-radio label="2" value="2">不优惠</el-radio>
            </div>
            <div style="margin-top:20px;">
              <el-radio label="1" value="1" :disabled="editdisabled">会员价</el-radio>
              <el-form-item label label-width="15px" style="display:inline-block;">
                <el-select filterable size="small" style="width:180px" :disabled="disablePriceone" v-model="ruleForm.memberpricelevelid" placeholder="全部" clearable>
                  <el-option v-for="item in commodityBrandList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
                <el-form-item style="display:inline-block;" label label-width="15px">
                  <span>服务类和次卡使用唯一的会员价</span>
                </el-form-item>
              </el-form-item>
            </div>
            <div style="margin-top:20px;">
              <el-radio label="0" value="0">会员折扣</el-radio>
              <el-input style="width:200px;" v-model="ruleForm.memberDiscount" size="small" placeholder="最多保留小数点后2位" clearable maxlength="8" :disabled="disablePrice"></el-input>
              <el-form-item style="display:inline-block;" label label-width="15px">
                <span>折 (打85折时填写8.5，服务类和次卡不参与会员折扣)</span>
              </el-form-item>
              <br />
            </div>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="升级奖励：" v-if="edutdata">
          <el-checkbox-group v-model="ruleForm.putChannel" @change="checkPoints" :disabled="editdisabled">
            <el-checkbox label="0" value="1">赠送积分</el-checkbox>
            <el-input style="width:200px;" v-model="ruleForm.rewardIntegral" size="small" placeholder="最多保留小数点后2位" clearable maxlength="8" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" :disabled="disablePrices"></el-input>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item :formatter="status" label="状 态" prop="resource2" v-if="edutdata">
          <el-radio-group v-model="ruleForm.status" :disabled="editdisabled">
            <el-radio label="0" value="0">启用</el-radio>
            <el-radio label="1" value="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item style="margin-left:300px;margin-top:90px">
          <el-button type="primary" v-if="isOwnAuth=='1'&&setRole('编辑')" @click="submitForm('ruleForm')">保存</el-button>
          <el-button v-if="isOwnAuth=='1'&&setRole('编辑')" @click="resetForm('ruleForm')">取消</el-button>
          <el-button v-else @click="resetForm('ruleForm')">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import u from "@/util";
import s from "@/util/state";
import { saveMemberLevel, goodsMemberPriceList } from "@/api";
import { mapState } from "vuex";
export default {
  computed: mapState(["sub", "power", "merchantId", "isOwnAuth"]),
  data() {
    return {
      id: "",
      ruleForm: {
        name: "",
        levelDescribe: "",
        discountType: "1",
        memberDiscount: "",
        memberpricelevelid: "",
        rewardIntegral: "",
        status: "0",
        putChannel: false
      },
      rules: {
        name: [
          { required: true, message: "请输入会员等级名称", trigger: "blur" },
          { min: 1, max: 30, message: "长度在 1到 30个字符", trigger: "blur" },
          { validator: u.valida, trigger: "blur" }
        ],
        memberDiscount: [
          { required: false, message: "请输入会员优惠", trigger: "blur" },
          {
            validator: u.validatenums,
            trigger: "blur",
            required: false
          }
        ]
      },
      disablePrice: true,
      disablePrices: true,
      disablePriceone: true,
      commodityBrandList: [],
      edutdata: true,
      editdisabled: false,
      editdisabled1: false,
      leveleNum: "" //会员数量
    };
  },
  mounted() {
    this.echo();
    this._goodsMemberPriceList();
    let type = u.getQuery("type");
    if (type == "1") {
      this.editdisabled = true;
      this.editdisabled1 = true;
    } else {
      this.editdisabled = false;
      this.editdisabled1 = false;
    }
  },
  methods: {
    integral: s.integral,
    status: s.status,
    //配置权限
    setRole(key) {
      return u.selectTitle(this.power, key);
    },
    //会员详细
    echo() {
      this.id = u.getQuery("id");
      let data = {};
      data = u.getQuery("detail");
      this.leveleNum = data.num;
      this.ruleForm.name = data.name;
      this.ruleForm.levelDescribe = data.levelDescribe;
      if (this.ruleForm.levelDescribe == "初始会员等级") {
        this.edutdata = false;
        this.editdisabled1 = true;
      }
      this.ruleForm.discountType = data.discountType.toString();
      if (this.ruleForm.discountType == "1") {
        this.disablePrice = true;
        this.disablePriceone = false;
      } else if (this.ruleForm.discountType == "0") {
        this.disablePriceone = true;
        this.disablePrice = false;
      } else {
        this.disablePriceone = true;
        this.disablePrice = true;
      }
      this.ruleForm.memberDiscount = data.memberDiscount;
      this.ruleForm.memberpricelevelid = data.memberPriceLevelId;
      this.ruleForm.rewardIntegral = data.rewardIntegral;
      if (this.ruleForm.rewardIntegral != "") {
        this.ruleForm.putChannel = true;
        this.disablePrices = false;
      }
      this.ruleForm.status = data.status.toString();
    },
    // 选择会员优惠
    changePrice(val) {
      if (val == "0") {
        this.disablePrice = false;
        this.disablePriceone = true;
        this.ruleForm.memberpricelevelid = "";
      } else if (val == "1") {
        this.disablePriceone = false;
        this.disablePrice = true;
        this.ruleForm.memberDiscount = "";
      } else {
        this.disablePriceone = true;
        this.disablePrice = true;
        this.ruleForm.memberDiscount = "";
        this.ruleForm.memberpricelevelid = "";
      }
    },
    //升级奖励
    checkPoints(val) {
      if (val == true) {
        this.disablePrices = false;
      } else {
        this.disablePrices = true;
      }
    },
    list() {
      let Data = {
        companyId: this.merchantId,
        name: this.ruleForm.name,
        levelDescribe: this.ruleForm.levelDescribe,
        discountType: this.ruleForm.discountType,
        memberDiscount: this.ruleForm.memberDiscount,
        memberPriceLevelId: this.ruleForm.memberpricelevelid,
        rewardIntegral: this.ruleForm.rewardIntegral,
        status: this.ruleForm.status,
        id: this.id
      };
      if (
        this.ruleForm.discountType == "1" &&
        this.ruleForm.memberpricelevelid == ""
      ) {
        this.$message({
          type: "error",
          message: "请选择会员价"
        });
      } else if (
        this.ruleForm.discountType == "0" &&
        this.ruleForm.memberDiscount == ""
      ) {
        this.$message({
          type: "error",
          message: "请输入会员折扣"
        });
      } else {
        if (this.leveleNum > 0) {
          if (this.ruleForm.status == "1") {
            this.$message({
              type: "error",
              message: "等级下有会员时不能停用"
            });
          } else {
            saveMemberLevel(Data).then(res => {
              if (res.code == 200) {
                this.$message({
                  type: "success",
                  message: res.msg
                });
                u.routerTo(
                  "/operate-management/member-management/member-level"
                );
              }
            });
          }
        } else {
          saveMemberLevel(Data).then(res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg
              });
              u.routerTo("/operate-management/member-management/member-level");
            }
          });
        }
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.list();
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      history.back();
    },
    //  会员等级
    _goodsMemberPriceList() {
      goodsMemberPriceList().then(res => {
        this.commodityBrandList = res.data;
      });
    }
  }
};
</script>
    <style scoped>
.from_main .el-input {
  width: 350px;
}
.inp {
  width: 400px;
  height: 40px;
  position: absolute;
  top: 3px;
  left: 340px;
}

.inp input {
  border: 1px solid #ccc;
  border-radius: 8px;
  line-height: 38px;
  text-indent: 6px;
  float: left;
}
.inp h1 {
  color: #606266;
  float: left;
  font-size: 14px;
}
.font h2 {
  width: 100px;
  position: absolute;
  top: 10px;
  left: 755px;
  color: #606266;
  font-size: 14px;
}
.font h6 {
  width: 130px;
  position: absolute;
  top: 0px;
  left: 270px;
  color: #606266;
  font-size: 12px;
  letter-spacing: 2px;
}
.font {
  margin-top: 20px;
  width: 350px;
}
</style>
<style lang="">
.memberStyle .el-form-item__error {
  left: 9% !important;
}
</style>
